<?php
define('IMAGE_QUALITY', 80); // 0-100, the larger the better & bigger
define('BACKGROUND_GRID_SIZE', 15); // chessboard like background

class ResizeImage {

	var $image;
	var $image_type;

	function load($filename) {
		if (!file_exists($filename)) return false;
		$image_info = getimagesize($filename);
		if (empty($image_info) || !$image_info[2])
			return false;
		
		$this->image_type = $image_info[2];
		if( $this->image_type == IMAGETYPE_JPEG ) {
			$this->image = imagecreatefromjpeg($filename);
		} elseif( $this->image_type == IMAGETYPE_GIF ) {
			$this->image = imagecreatefromgif($filename);
		} elseif( $this->image_type == IMAGETYPE_PNG ) {
			$this->image = imagecreatefrompng($filename);
		}
		
		return true;
	}
	function save($filename, $image_type=IMAGETYPE_JPEG, $compression=IMAGE_QUALITY, $permissions=null) {
		if( $image_type == IMAGETYPE_JPEG ) {
			imagejpeg($this->image,$filename,$compression);
		} elseif( $image_type == IMAGETYPE_GIF ) {
			imagegif($this->image,$filename);
		} elseif( $image_type == IMAGETYPE_PNG ) {
			imagepng($this->image,$filename);
		}
		if( $permissions != null) {
			chmod($filename,$permissions);
		}
		
		return true;
	}
	function output($image_type=IMAGETYPE_JPEG) {
		if( $image_type == IMAGETYPE_JPEG ) {
			imagejpeg($this->image);
		} elseif( $image_type == IMAGETYPE_GIF ) {
			imagegif($this->image);
		} elseif( $image_type == IMAGETYPE_PNG ) {
			imagepng($this->image);
		}
	}

	function getWidth() {
		return imagesx($this->image);
	}

	function getHeight() {
		return imagesy($this->image);
	}

	function scale($scale) {
		$new_width = $this->getWidth() * $scale/100;
		$new_height = $this->getHeight() * $scale/100;
		$this->resize($new_width, $new_height);
	}

	function resize($new_width, $new_height) {
		$new_image = imagecreatetruecolor($new_width, $new_height);
		$width = $this->getWidth();
		$height = $this->getHeight();
		imagecopyresampled($new_image, $this->image, 0, 0, 0, 0,
			$new_width, $new_height, $width, $height);
		$this->image = $new_image;
	}
	
	function autoFullFit($width=400, $height=400) {
		$new_width = $this->getWidth();
		$new_height = $this->getHeight();
		
		if ($this->getWidth() < $this->getHeight())
		{
			$scale = $width/$this->getWidth();
			$offset_x = 0;
			$offset_y = ($this->getHeight()*$scale-$height)/2;
		}
		else
		{
			$scale = $height/$this->getHeight();
			$offset_x = ($this->getWidth()*$scale-$width)/2;
			$offset_y = 0;
		}

		$this->scale($scale*100);

		$new_image = imagecreatetruecolor($width, $height);
		imagecopyresized($new_image, $this->image, 0, 0, $offset_x, $offset_y,
			$width, $height, $width, $height);
		$this->image = $new_image;
	}

	function autoResize($max_width=400, $max_height=400, $min_width=50, $min_height=50)
	{
		$flag_rotated = false;
		
		// if landscape then put it to portrait - so i can focus on a portrait one
		if ($this->getWidth() > $this->getHeight())
		{
			$this->image = imagerotate($this->image, 90, 0);
			$flag_rotated = true;
		}
		
		$width = $this->getWidth();
		$height = $this->getHeight();

		$new_width = 0;
		$new_height = 0;

		// tiny
		if ($width<$min_width && $height<$min_height)
		{
			$new_image = imagecreatetruecolor($min_width, $min_height);
			$new_image = $this->drawGridBackground($new_image);
			imagecopyresampled($new_image, $this->image,
				($min_width-$width)/2, ($min_height-$height)/2, 0, 0,
				$width, $height, $width, $height);
			$this->image = $new_image;
		}
		else
		{
			$this->scale(100*$max_height/$height);
		}
		
		$width = $this->getWidth();
		$height = $this->getHeight();

		// portrait too narrow, need slice
		if ($width<$min_width && $height>=$max_height)
		{
			$dst_x = ($min_width-$width)/2;
			$dst_y = 0;
			
			$src_x = 0;
			$src_y = ($height-$max_height)/2;
			
			$dst_w = $width;
			$dst_h = $max_height;
			
			$src_w = $width;
			$src_h = $max_height;
			
			$new_width = $min_width;
			$new_height = $max_height;
			
			//echo "$new_width, $new_height\n";
			//echo "$dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h\n";
			
			$new_image = imagecreatetruecolor($new_width, $new_height);
			$new_image = $this->drawGridBackground($new_image);
			imagecopyresampled($new_image, $this->image,
				$dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
			$this->image = $new_image;
		}
	
		// if rotated then rotate it back
		if ($flag_rotated)
		{
			$this->image = imagerotate($this->image, -90, 0);
		}
		
		return true;

	}
	
	function drawGridBackground(&$image)
	{
		$white = imagecolorallocate($image, 255, 255, 255);
		$gray = imagecolorallocate($image, 200, 200, 200);
		for ($x=0; $x<imagesx($image); $x+=BACKGROUND_GRID_SIZE)
			for ($y=0; $y<imagesy($image); $y+=BACKGROUND_GRID_SIZE)
			{
				$color = ($x/BACKGROUND_GRID_SIZE+$y/BACKGROUND_GRID_SIZE+1)%2 == 1 ? $gray : $white;
				imagefilledrectangle($image, $x, $y,
					$x+BACKGROUND_GRID_SIZE, $y+BACKGROUND_GRID_SIZE, $color);
			}
			
		return $image;
	}

}
?>